﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Diagnostics;

namespace SpriteExtractor.ViewModel
{

    

    public class SpriteViewModel : ViewModelBase
    {

        public Model.Sprite Sprite { get; protected set; }
        public bool EditingMode { get; set; }
        ImageViewModel _imageViewModel;

        public SpriteViewModel(ImageViewModel imageViewModel, Model.Sprite sprite)
        {
            _imageViewModel = imageViewModel;
            Sprite = sprite;
        }

        public Rect Rect 
        { 
            get 
            { 
                return Sprite.Rect; 
            } 
            set 
            {
                Sprite.Rect = value;
                OnPropertyChanged("Rect"); 
                OnPropertyChanged("Anchor");
            } 
        }
        public double Width 
        { 
            get { return Rect.Width; } 
            set { Rect = new Rect(Rect.X, Rect.Y, value, Rect.Height); } 
        }
        public double Height
        {
            get { return Rect.Height; }
            set { Rect = new Rect(Rect.X, Rect.Y, Rect.Width, value); }
        }

        public Point Anchor { get { return DenormAnchor(Sprite.Anchor); } set { Sprite.Anchor = NormAnchor(value); OnPropertyChanged("Anchor"); } }
        public Point AnchorRelative { get { return new Point(Math.Round(Sprite.Anchor.X, 2), Math.Round(Sprite.Anchor.Y, 2)); } set { Sprite.Anchor = value; OnPropertyChanged("Anchor"); } }
        public string Title { get { return Sprite.Title; } set { Sprite.Title = value; OnPropertyChanged("Title"); } }

        private Point NormAnchor(Point anchor)
        {
            return new Point(anchor.X / Sprite.Rect.Width, anchor.Y / Sprite.Rect.Height);
        }
        private Point DenormAnchor(Point anchor)
        {
            return new Point(anchor.X * Sprite.Rect.Width, anchor.Y * Sprite.Rect.Height);
        }

        public List<CommandViewModel> Commands
        {
            get { return new List<CommandViewModel>() { RemoveSpriteCommand }; }
        }

        public CommandViewModel MoveCommand
        {
            get { return new CommandViewModel("Move", new RelayCommand(param=>this.Rect=(Rect)param));  }
        }
        public CommandViewModel RemoveSpriteCommand
        {
            get { return _imageViewModel.RemoveSpriteCommand;  }
        }

        public CommandViewModel MoveAnchor
        {
            get { return new CommandViewModel("Move Anchor", new RelayCommand(direction=>this.MoveAnchorImpl(direction as string)));  }
        }

        public override string ToString()
        {
            return Sprite.ToString();
        }

        private void MoveAnchorImpl(string direction)
        {
            switch (direction)
            {
                case "NW":
                    { AnchorRelative = new Point(0, 0); break; }
                case "N":
                    { AnchorRelative = new Point(0.5, 0); break; }
                case "NE":
                    { AnchorRelative = new Point(1, 0); break; }
                case "W":
                    { AnchorRelative = new Point(0, 0.5); break; }
                case "C":
                    { AnchorRelative = new Point(0.5, 0.5); break; }
                case "E":
                    { AnchorRelative = new Point(1, 0.5); break; }
                case "SW":
                    { AnchorRelative = new Point(0, 1); break; }
                case "S":
                    { AnchorRelative = new Point(0.5, 1); break; }
                case "SE":
                    { AnchorRelative = new Point(1, 1); break; }
                default:
                    {
                        Debug.Assert(false);
                        break;
                    }
            }
        }
    }
}
